package cn.genmer.test.security.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

//@Aspect
//@Component
public class AspectService {

    private Logger logger = LoggerFactory.getLogger(AspectService.class);


    public static final String ANSI_RESET = "\u001B[0m";

    public static final String ANSI_BLACK = "\u001B[30m";

    public static final String ANSI_RED = "\u001B[31m";

    public static final String ANSI_GREEN = "\u001B[32m";

    public static final String ANSI_YELLOW = "\u001B[33m";

    public static final String ANSI_BLUE = "\u001B[34m";

    public static final String ANSI_PURPLE = "\u001B[35m";

    public static final String ANSI_CYAN = "\u001B[36m";

    public static final String ANSI_WHITE = "\u001B[37m";

    /**
     * 此表达式的含义
     * 		1.第一个* 表示访问的权限为任意权限
     * 		2.第二个* 表示返回值的类型为任意类型
     * 		3.第三个.. 表示包前两的路径 完整的写法应该是 com.sxt.serviceImpl.ServiceIntface
     * 		4.第四个 是指定那个的方法被调用的时候指定before方法
     * 		5.第五个（..）表示任意参数
     */
//    @Pointcut("@annotation(cn.genmer.test.security.annotation.HyperLogInc)")
    @Pointcut(value = "execution(* cn.genmer.test.security.controller.a.*.*(..))")
//    @Pointcut("execution(public * *(..))")
    public void pointcut(){
    }

    @Before("pointcut()")
    public void before(JoinPoint joinPoint) throws Throwable {
//        logger.info("执行前 : {}", joinPoint.getClass());

    }

    @After("pointcut()")
    public void after(JoinPoint joinPoint){
//        logger.info("执行后 : {}", joinPoint.getClass());
    }

    @Around("pointcut()")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        long start = System.currentTimeMillis();
        logger.info("执行前 : {}", joinPoint.getClass());
        joinPoint.proceed();
        logger.info("执行后 : {}", joinPoint.getClass());
//        TimeUnit.MINUTES.sleep(1);
        TimeUnit.MILLISECONDS.sleep(200);
        long end = System.currentTimeMillis();
        System.out.println(String.format("%s执行时间： %s %s",ANSI_GREEN, timeUnitTransfer(end-start),ANSI_GREEN));
        return;
    }

    /**
     * 毫秒转换分秒
     * @param millis
     * @return
     */
    public static String timeUnitTransfer(long millis){
        if (millis < 1000){
            return String.format("%sms", millis);
        } else if (millis < 10000){
            return String.format("%ss %sms", TimeUnit.MILLISECONDS.toSeconds(millis), millis%1000);
        }else {

        }
        return String.format("%smin %ss", TimeUnit.MILLISECONDS.toMinutes(millis), (millis / 1000) % 60);
    }

}
